"""
线程
对于全局共享变量，可实现线程通信，所有线程共享
共享数据由于多线程会出现数据错误，需要加锁GIL
python底层自带GIL锁，但是在达到一定计算量时会自动解除
"""
import threading
from time import sleep
import random

# def downlod(n):
#     images = ['girl.jpg', 'boy.jpg', 'man.jpg']
#     for images in images:
#         print('正在下载', images)
#         sleep(0.5)
#         print(images, '下载成功')
#
#
# def listenmusic():
#     musics = ['大碗宽面', '土耳其冰激凌', '烤面筋', '烤馒头片']
#     for music in musics:
#         sleep(0.5)
#         print('正在听{}歌'.format(music))
#
#
# if __name__ == '__main__':
#     # 创建线程
#     t1 = threading.Thread(target=downlod, name='aa', args=(1,))
#     t1.start()
#
#     t2 = threading.Thread(target=listenmusic, name='aa')
#     t2.start()

# n = 1
# while True:
#     print(n)
#     sleep(1.5)
#     n += 1

'''
同步：一个一个完成，一个做完另一个才能进来
'''
# lock = threading.Lock()  # 定义锁
#
# list1 = [0] * 10
#
#
# def task1():
#     lock.acquire()  # 请求上锁
#     for i in range(len(list1)):
#         list1[i] = 1
#         sleep(0.5)
#     lock.release()  # 释放锁
#
#
# def task2():
#     lock.acquire()  # 请求上锁
#     for i in range(len(list1)):
#         print('------', list1[i])
#         sleep(0.5)
#     lock.release()
#
#
# if __name__ == '__main__':
#     t1 = threading.Thread(target=task1)
#     t2 = threading.Thread(target=task2)
#
#     t1.start()
#     t2.start()
